﻿<template>
  <div class="l-rblock l-form-viewer" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="0">
              <div class="l-rblock">
                <el-col
                  v-if="lr_formLookAuth('case_erp_unit_f_UnitTypeId')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('单位类型')"
                    prop="case_erp_unit_f_UnitTypeId"
                  >
                    <l-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_unit_f_UnitTypeId',
                          false || isReadOnly
                        )
                      "
                      :options="case_erp_unit_f_UnitTypeIdOptions"
                      v-model="formData.case_erp_unit_f_UnitTypeId"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_unit_f_Name')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('单位名称')"
                    prop="case_erp_unit_f_Name"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth('case_erp_unit_f_Name', isReadOnly)
                      "
                      v-model="formData.case_erp_unit_f_Name"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_unit_f_Symbol')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('单位符号')"
                    prop="case_erp_unit_f_Symbol"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth('case_erp_unit_f_Symbol', isReadOnly)
                      "
                      v-model="formData.case_erp_unit_f_Symbol"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_unit_f_IsStandard')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('基本单位')"
                    prop="case_erp_unit_f_IsStandard"
                  >
                    <l-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_unit_f_IsStandard',
                          false || isReadOnly
                        )
                      "
                      :options="isStandardOptions"
                      v-model="formData.case_erp_unit_f_IsStandard"
                    />
                  </el-form-item>
                </el-col>

                <el-col
                  v-if="lr_formLookAuth('case_erp_unit_f_State')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('状态')"
                    prop="case_erp_unit_f_State"
                  >
                    <el-switch
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_unit_f_State',
                          false || isReadOnly
                        )
                      "
                      :active-value="0"
                      :inactive-value="1"
                      v-model="formData.case_erp_unit_f_State"
                    >
                    </el-switch>
                  </el-form-item>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.unit;
export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      moduleCode: "ERP_Units",
      activeName: "tab0",
      formData: {
        case_erp_unit_f_UnitTypeId: "", // 单位类型
        case_erp_unit_f_Name: "", // 单位名称
        case_erp_unit_f_Symbol: "", // 单位符号
        case_erp_unit_f_IsStandard: 1, // 是否基准单位
        case_erp_unit_f_State: 0, // 状态
      },
      rules: {
        case_erp_unit_f_UnitTypeId: [{ required: true, message: "请选择" }],
        case_erp_unit_f_Name: [
          { required: true, message: "请输入" },
          {
            validator: this.lr_existDbFiled,
            fieldName: "f_Name",
            keyValue: () => {
              return this.formData.case_erp_unit_f_Id;
            },
            tableName: "case_erp_unit",
            keyName: "f_Id",
          },
        ],
        case_erp_unit_f_Symbol: [
          { required: true, message: "请输入" },
          {
            validator: this.lr_existDbFiled,
            fieldName: "f_Symbol",
            keyValue: () => {
              return this.formData.case_erp_unit_f_Id;
            },
            tableName: "case_erp_unit",
            keyName: "f_Id",
          },
        ],
        case_erp_unit_f_IsStandard: [{ required: true, message: "请选择" }],
      },
      // 是否基准单位
      isStandardOptions: [
        { value: 0, label: "是" },
        { value: 1, label: "否" },
      ],
    };
  },
  computed: {
    case_erp_unit_f_UnitTypeIdOptions() {
      const data = this.lr_DataSource("ERP_UnitTypes") || [];
      return this.lr_DataSourceOptions(data.filter(t=>t.f_state == 0), "f_id", "f_name");
    },
  },
  mounted() {},
  methods: {
    // 重置表单
    resetForm() {
      this.formData.case_erp_unit_f_Id = "";
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_unit");
    },
    // 获取表单数据
    getForm() {
      return this.$getFormData(this.formData, "case_erp_unit");
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // 新增
        postData.f_Id = keyValue;

        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>